/// <reference name="MicrosoftAjax.js"/>

(function () {
    Type.registerNamespace("Sys");
    Type.registerNamespace("Sys.Extended");
    Type.registerNamespace("Sys.Extended.UI");

    var scriptName = "AdPanel";

    if (Type._registerScript) {
        Type._registerScript("AdPanel.js");
    }

    function execute() {

    	Sys.Extended.UI.AdPanelContent = function (){
    	    throw Error.invalidOperation();
    	};
    	Sys.Extended.UI.AdPanelContent.prototype = {
    		Content: 0,
		    Wrap: 1
		};
		Sys.Extended.UI.AdPanelContent.registerEnum("Sys.Extended.UI.AdPanelContent", false);

		Sys.Extended.UI.AdPanelPosition = function (){
    	    throw Error.invalidOperation();
    	};
    	Sys.Extended.UI.AdPanelPosition.prototype = {
    		Top: 0,
		    Left: 1,
		    Bottom: 2,
		    Right: 3
		};
		Sys.Extended.UI.AdPanelPosition.registerEnum("Sys.Extended.UI.AdPanelPosition", false);
    	
		Sys.Extended.UI.AdPanelLayer = function (){
    	    throw Error.invalidOperation();
    	};
    	Sys.Extended.UI.AdPanelLayer.prototype = {
    		Inline: 0,
    		InlineBlock: 1,
    		Block: 2
		};
		Sys.Extended.UI.AdPanelLayer.registerEnum("Sys.Extended.UI.AdPanelLayer", false);

		Sys.Extended.UI.AdWidth = function (){
    	    throw Error.invalidOperation();
    	};
    	Sys.Extended.UI.AdWidth.prototype = {
    		Auto: 0,
		    Inherit: 1,
		    Reponsive: 2
		};
		Sys.Extended.UI.AdWidth.registerEnum("Sys.Extended.UI.AdWidth", false);
    	
        Sys.Extended.UI.AdPanel = function (element){
            Sys.Extended.UI.AdPanel.initializeBase(this, [element]);

            this._Title = "Partners";
            this._Width = 240;
            this._Height = 'auto';
            this._Content = Sys.Extended.UI.AdPanelContent.Content;
            this._Layer = Sys.Extended.UI.AdPanelLayer.InlineBlock;
            this._Position = Sys.Extended.UI.AdPanelPosition.Right;
            this._Ads = [];
            
            this._delegates = {};
            
        };
        Sys.Extended.UI.AdPanel.prototype = {
            initialize: function (){
                Sys.Extended.UI.AdPanel.callBaseMethod(this, 'initialize');

                if (this._Ads.length > 0){
                	this._createAdpanel();
                }
            },

            dispose: function (){
        		if (this._delegates.resize){
        			var el = this.get_element();
        			$removeHandler(el.parentNode, "click", this._delegates.resize);
        			this._delegates.resize = null;
        		}
            	
                Sys.Extended.UI.AdPanel.callBaseMethod(this, "dispose");
            },

            get_Title: function (){ return this._Title; },
            set_Title: function (value){ this._Title = value; },
            get_Width: function (){ return this._Width; },
            set_Width: function (value){ this._Width = value; },
            get_Height: function (){ return this._Height; },
            set_Height: function (value){ this._Height = value; },
            get_Content: function (){ return this._Content; },
            set_Content: function (value){ this._Content = value; },
            get_Layer: function (){ return this._Layer; },
            set_Layer: function (value){ this._Layer = value; },
            get_Position: function (){ return this._Position; },
            set_Position: function (value){ this._Position = value; },
            get_Ads: function (){ return this._Ads; },
            set_Ads: function (value){
            	if (Array.isArray(value)) {
            		this._Ads = value;
            	} 
            },
            
            _getSize: function (data){
            	/// <summary>
                /// Calculate size for the style.width and style.height attributes of a DomElement
                /// </summary>
                /// <param name="data" type="Object" optional="true" mayBeNull="true">Value of size</param>
                /// <returns type="String" />
                var i, rx, rxtest, newsize = data, size = 'auto';
            	
            	if (!data || (data === 0) || (data == '0')){
            		newsize = 'auto';
            	}
            	
            	newsize = String.format("{0}", newsize);
            	
            	if (!isNaN(parseInt(newsize, 10))) {
            		size = newsize + "px";
            	}
            	else {
            		rxtest = ["/^[0-9\\.]+(px){0,}$","^[0-9\\.]+pt$","^[0-9\\.]+em$","^[0-9\\.]+%$"];
            		
            		for (i=0; i<rxtest.length; i++){
            			rx = new RegExp(rxtest[i]);
            			
            			if (rx.test(newsize)){
                       		size = newsize;   
                       		break;
            			}
            		}
            	}
            	
            	return size;
            },
            
            _setSize: function (el){
            	/// <summary>
                /// Set size of the element in the parameter
                /// </summary>
                /// <param name="el" type="Sys.UI.DomElement" optional="false" mayBeNull="false">A DomElement for setting size</param>
            	var width = this._getSize(this._Width);
            	var height = this._getSize(this._Height);
            	
            	el.style.height = height;
            	el.style.width = width;
            	
            	if (height != 'auto') {
            		el.style.overflow = 'auto';
            		el.style.overflowX = 'hidden';
            	}
            },
            
            _setWrapStyle: function (){
            	/// <summary>
                /// Set wrap element style from the targetcontrol
                /// </summary>
            	var el,wrap,styleprops,i;
            	
            	el = this.get_element();
            	wrap = el.parentNode;
            	styleprops = "display,cssFloat,position,width,height";
            	styleprops = styleprops.split(',');
            	
            	for (i=0;i<styleprops.length;i++){
            		wrap.style[styleprops[i]] = el.style[styleprops[i]];
            	}
            	
            	wrap.className = el.className;
            	el.className = "";
            }, 
            
            _onResize: function (){
            	var el = this.get_element();
            	var wrapContentSize = $common.getContentSize(el.parentNode);
            	var adPanelSize = $common.getSize($get(String.format("{0}_adPanel", el.id)));
            	var width = wrapContentSize.width - adPanelSize.width - 30;
            	
            	el.style.width = width + "px";
            }, 
            
            _setResize: function (){
            	var el = this.get_element();
            	
            	this._delegates.resize = Function.createDelegate(this, this._onResize);
            	$addHandler(el.parentNode, "click", this._delegates.resize);
            	
            	this._onResize();
            },
            
            _createAdpanel: function (){
            	/// <summary>
                /// Create a panel for the advertisenments
                /// </summary>
            	var target, el, div, h3;
            	
            	target = this.get_element();
            	el = target;
            	
            	if (this._Content == Sys.Extended.UI.AdPanelContent.Wrap){
            		// wrap the targetcontrol
            		div = document.createElement("div");
            		$common.wrapElement(el, div);
            		el = document.createElement("div");
            		el.id = String.format("{0}_adPanel", target.id);
            		el.style.margin = "5px";
            		
            		this._setSize(el);
            		this._setWrapStyle();
            		
            		// AdPanel position
            		if (this._Position == Sys.Extended.UI.AdPanelPosition.Top){
            			target.parentNode.insertBefore(el, target);
            		}
            		else if (this._Position == Sys.Extended.UI.AdPanelPosition.Bottom){
                		target.parentNode.appendChild(el);
            		}
            		else {
            			target.parentNode.insertBefore(el, target);
            			
            			el.style.display = "inline-block";
            			target.style.display = "inline-block";
            			
            			if (this._Position == Sys.Extended.UI.AdPanelPosition.Left){
            				el.style.cssFloat = "left";
            				target.style.cssFloat = "right";
            			}
            			else if (this._Position == Sys.Extended.UI.AdPanelPosition.Right){
            				el.style.cssFloat = "right";
            				target.style.cssFloat = "left";
            			}
            			
            			this._setResize();
            		}
            	}
            	else {
            		this._setSize(el);
            	}
            	
            	// Format the AdPanel as bootstrap panel
            	Sys.UI.DomElement.addCssClass(el, "panel panel-primary");
            	
            	if (this._Title){
            		div = document.createElement("div");
            		Sys.UI.DomElement.addCssClass(div, "panel-heading");
            		
            		h3 = document.createElement("h3");
            		Sys.UI.DomElement.addCssClass(h3, "panel-title");
            		h3.innerHTML = this._Title;
            		
            		div.appendChild(h3);
            		el.appendChild(div);
            	}
            	
            	this._appendAdItems(el);
            },
            
            _appendAdItems: function (el){
            	var i, div, image, link, adItems, contentSize;
            	
            	adItems = this._Ads;
            	contentSize = $common.getContentSize(el);
            	
            	for (i=0; i< adItems.length; i++){
            		link = null;
            		image = null;
            		div = document.createElement("div");
            		
            		div.style.width = "auto";
            		div.style.height = "auto";
            		
            		if (this._Layer == Sys.Extended.UI.AdPanelLayer.Inline){
            			div.style.display = "inline";
            			div.style.paddingLeft = "3px";
            			div.style.height = contentSize.height + "px";
            		}
            		else if (this._Layer == Sys.Extended.UI.AdPanelLayer.InlineBlock){
            			div.style.display = "inline-block";
            			div.style.paddingLeft = "3px";
            			div.style.height = contentSize.height + "px";
            		}
            		else {
            			div.style.paddingTop = "3px";
            			div.style.width = contentSize.width + "px";
            		}
            		
            		if (adItems[i].link){
                		link = document.createElement("a");
                		link.setAttribute("href", adItems[i].link);
                		link.setAttribute("target", "_blank");
                		link.style.width = "inherit";
                		div.appendChild(link);
            		}
            		
            		if (adItems[i].image){
                		image = document.createElement("img");
                		image.src = adItems[i].image;
                		
                		if (adItems[i].width){
                			if (adItems[i].width == Sys.Extended.UI.AdWidth.Reponsive){
                				Sys.UI.DomElement.addCssClass(image, "img-responsive");
                			}
                			else {
                				image.style.width = "inherit";
                			}
                		}
                		
                		if (adItems[i].tooltip){
                    		image.setAttribute("alt", adItems[i].tooltip);
                		}
                		else {
                    		image.setAttribute("alt", "");
                		}
                		
                		if (link){
                			link.appendChild(image);
                		}
                		else {
                			div.appendChild(image);
                		}
            		}
            		
            		el.appendChild(div);
            		
            		if (adItems[i].content){
            			jQuery(div).append(jQuery(String.format("<div>{0}<div>", adItems[i].content)));
            		}
            		
            		if (adItems[i].tooltip){
            			div.setAttribute("title", adItems[i].tooltip);
            			
            			if (this._Position == Sys.Extended.UI.AdPanelPosition.Left){
            				div.setAttribute("data-placement", "right");
            			}
            			else if (this._Position == Sys.Extended.UI.AdPanelPosition.Right){
            				div.setAttribute("data-placement", "left");
            			}
            			else if (this._Position == Sys.Extended.UI.AdPanelPosition.Top){
            				div.setAttribute("data-placement", "bottom");
            			}
            			else {
            				div.setAttribute("data-placement", "top");
            			}
            			
            			jQuery(div).tooltip();
            		}
            	}
            }
        };

        if (Sys.Extended.UI.BehaviorBase){
            Sys.Extended.UI.AdPanel.registerClass('Sys.Extended.UI.AdPanel', Sys.Extended.UI.BehaviorBase);
        }
        else {
            Sys.Extended.UI.AdPanel.registerClass('Sys.Extended.UI.AdPanel', Sys.UI.Behavior);
        }
        if (Sys.registerComponent){ Sys.registerComponent(Sys.Extended.UI.AdPanel, { name: 'AdPanel' }); }


    } // execute

    if (window.Sys && Sys.loader){
        Sys.loader.registerScript(scriptName, ["ExtendedBase", "ExtendedCommon"], execute);
    }
    else {
        execute();
    }
})();